package com.docker.sys.app;

import com.docker.core.mvc.service.BaseService;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Okv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import kit.docker.convert.RecordKit;

import java.util.List;

/**
 * @Author ldl
 * @Create 2020/09/18 018 14:17
 */
public class AppService extends BaseService {

    @Inject
    private AppDao appDao;

    public Page<Record> paginate(Okv cond, int pageNum, int pageSize) {
        return appDao.paginate(cond, pageNum, pageSize);
    }

    public List<Record> look(Okv cond) {
        return appDao.look(cond);
    }

    public boolean save(AppBean bean, String userId) {
        bean.setApp_id(getUUID32());
        bean.setStatus_flag("1");
        bean.setApp_secret(getUUID32());
        bean.setCreate_time(getNowDateTime());
        bean.setCreate_oper_id(userId);
        bean.setUpdate_time(bean.getCreate_time());
        bean.setUpdate_oper_id(userId);
        return appDao.save(RecordKit.objectToRecord(bean));
    }

    public boolean update(AppBean bean, String appIdEx, String userId) {
        Record app = appDao.info(appIdEx);
        if (!StrKit.notNull(app)) {
            throwError("应用不存在");
        }
        app.set("app_name", bean.getApp_name());
        app.set("expire_time", bean.getExpire_time());
        app.set("user_token", bean.getUser_token());
        app.set("user_type", bean.getUser_type());
        app.set("add_secret", bean.getAdd_secret());
        app.set("token_expire", bean.getToken_expire());
        app.set("restrict_access", bean.getRestrict_access());
        app.set("restrict_platform", bean.getRestrict_platform());
        app.set("update_time", getNowDateTime());
        app.set("update_oper_id", userId);
        return appDao.update(app);
    }

    public Record secret(String appIdEx) {
        return appDao.secter(appIdEx);
    }

    public boolean cache() {
        Okv cond = Okv.create().set("a.status_flag = ", "1");
        List<Record> apps = appDao.list(Okv.by("cond", cond));
        AppRedis.clear();
        for (Record rec : apps) {
            AppRedis.save(rec);
        }
        return true;
    }

}
